package org.market.domain.strategy.service.rule.tree.engine.impl;

import org.market.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import org.market.domain.strategy.model.valobj.RuleTreeNodePathVO;
import org.market.domain.strategy.model.valobj.RuleTreeNodeVO;
import org.market.domain.strategy.model.valobj.RuleTreeVO;
import org.market.domain.strategy.service.rule.tree.IRuleTreeNode;
import org.market.domain.strategy.service.rule.tree.engine.IDecisionEngine;
import org.market.domain.strategy.service.rule.tree.factory.DefaultRuleTreeFactory;

import java.util.List;
import java.util.Map;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/25 下午10:17
 * @Version: 1.0
 */

public class DecisionEngin implements IDecisionEngine {
    //装着所有节点的容器,key为对应的容器对象
    protected final Map<String, IRuleTreeNode> treeNodeGroup;
    //规则树的框架:根(root)节点和RuleTreeVO子节点的容器Map
    protected final RuleTreeVO treeVO;

    public DecisionEngin(Map<String, IRuleTreeNode> treeNodeGroup, RuleTreeVO tree) {
        this.treeNodeGroup = treeNodeGroup;
        this.treeVO = tree;
    }

    @Override
    public DefaultRuleTreeFactory.StrategyAwardVO process(String userId, Long StrategyId, Integer awardId) {
        DefaultRuleTreeFactory.StrategyAwardVO strategyAwardVO = null;

        //获取根节点
        String node = treeVO.getTreeRootNode();
        //RuleTreeVO子节点的容器Map
        Map<String, RuleTreeNodeVO> map = treeVO.getNodeGroup();

        //获取RuleTreeVO节点对象
        RuleTreeNodeVO ruleTreeNodeVO = map.get(node);
        while (node != null){
            //节点容器get RuleTreeNodeVO的key返回注册器
            IRuleTreeNode logicConstructor = treeNodeGroup.get(ruleTreeNodeVO.getKey());
            //获取ruleValue
            String ruleValue = ruleTreeNodeVO.getValue();
            //通过参数完成注册,返回规则执行结果
            DefaultRuleTreeFactory.RuleActionEntity ruleActionEntity = logicConstructor.logic(userId, StrategyId, awardId,ruleValue);
            //规则执行结果取出过滤校验type(allow/takeover)
            RuleLogicCheckTypeVO ruleLogicCheckType = ruleActionEntity.getRuleLogicCheckType();
            //获取当前策略奖励数据,直到规则树流程走完,获取最终结果
            strategyAwardVO = ruleActionEntity.getStrategyAwardVO();
            //根据路径和条件到下一个节点
            node = nextNode(ruleLogicCheckType.getCode(), ruleTreeNodeVO.getPathList());
            //获取下一个节点的VO对象
            ruleTreeNodeVO = map.get(node);
        }
        return strategyAwardVO;
    }

    public String nextNode(String matterValue, List<RuleTreeNodePathVO> pathList){
        if (pathList == null ||pathList.isEmpty()) return null;
        for (RuleTreeNodePathVO path : pathList) {
            if (isDecision(matterValue,path)){
                return path.getTo();
            }
        }
        //throw new RuntimeException("决策树引擎");
        return null;
    }
    public static boolean isDecision(String matterValue, RuleTreeNodePathVO path){
        switch (path.getRuleLimitType()){
            case EQUAL:
                return matterValue.equals(path.getLogicLimitValue().getCode());
            case GT:
            case LT:
            case GE:
            case LE:
            default:
                return false;
        }
    }
}
